home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / firefox / components / nsBrowserContentHandler.js < prev    next >
Encoding:
Text File  |  2007-04-03  |  27.0 KB  |  810 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is the Mozilla Firefox browser.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Benjamin Smedberg <benjamin@smedbergs.us>
  18.  *
  19.  * Portions created by the Initial Developer are Copyright (C) 2004
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  26.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38. const nsISupports            = Components.interfaces.nsISupports;
  39.  
  40. const nsIBrowserDOMWindow    = Components.interfaces.nsIBrowserDOMWindow;
  41. const nsIBrowserHandler      = Components.interfaces.nsIBrowserHandler;
  42. const nsIBrowserHistory      = Components.interfaces.nsIBrowserHistory;
  43. const nsIChannel             = Components.interfaces.nsIChannel;
  44. const nsICommandLine         = Components.interfaces.nsICommandLine;
  45. const nsICommandLineHandler  = Components.interfaces.nsICommandLineHandler;
  46. const nsIContentHandler      = Components.interfaces.nsIContentHandler;
  47. const nsIDocShellTreeItem    = Components.interfaces.nsIDocShellTreeItem;
  48. const nsIDOMChromeWindow     = Components.interfaces.nsIDOMChromeWindow;
  49. const nsIDOMWindow           = Components.interfaces.nsIDOMWindow;
  50. const nsIFactory             = Components.interfaces.nsIFactory;
  51. const nsIFileURL             = Components.interfaces.nsIFileURL;
  52. const nsIHttpProtocolHandler = Components.interfaces.nsIHttpProtocolHandler;
  53. const nsIInterfaceRequestor  = Components.interfaces.nsIInterfaceRequestor;
  54. const nsIPrefBranch          = Components.interfaces.nsIPrefBranch;
  55. const nsIPrefLocalizedString = Components.interfaces.nsIPrefLocalizedString;
  56. const nsISupportsString      = Components.interfaces.nsISupportsString;
  57. const nsIURIFixup            = Components.interfaces.nsIURIFixup;
  58. const nsIWebNavigation       = Components.interfaces.nsIWebNavigation;
  59. const nsIWindowMediator      = Components.interfaces.nsIWindowMediator;
  60. const nsIWindowWatcher       = Components.interfaces.nsIWindowWatcher;
  61. const nsICategoryManager     = Components.interfaces.nsICategoryManager;
  62. const nsIWebNavigationInfo   = Components.interfaces.nsIWebNavigationInfo;
  63. const nsIBrowserSearchService = Components.interfaces.nsIBrowserSearchService;
  64.  
  65. const NS_BINDING_ABORTED = 0x804b0002;
  66. const NS_ERROR_WONT_HANDLE_CONTENT = 0x805d0001;
  67. const NS_ERROR_ABORT = Components.results.NS_ERROR_ABORT;
  68.  
  69. function shouldLoadURI(aURI) {
  70.   if (aURI && !aURI.schemeIs("chrome"))
  71.     return true;
  72.  
  73.   dump("*** Preventing external load of chrome: URI into browser window\n");
  74.   dump("    Use -chrome <uri> instead\n");
  75.   return false;
  76. }
  77.  
  78. function resolveURIInternal(aCmdLine, aArgument) {
  79.   var uri = aCmdLine.resolveURI(aArgument);
  80.  
  81.   if (!(uri instanceof nsIFileURL)) {
  82.     return uri;
  83.   }
  84.  
  85.   try {
  86.     if (uri.file.exists())
  87.       return uri;
  88.   }
  89.   catch (e) {
  90.     Components.utils.reportError(e);
  91.   }
  92.  
  93.   // We have interpreted the argument as a relative file URI, but the file
  94.   // doesn't exist. Try URI fixup heuristics: see bug 290782.
  95.  
  96.   try {
  97.     var urifixup = Components.classes["@mozilla.org/docshell/urifixup;1"]
  98.                              .getService(nsIURIFixup);
  99.  
  100.     uri = urifixup.createFixupURI(aArgument, 0);
  101.   }
  102.   catch (e) {
  103.     Components.utils.reportError(e);
  104.   }
  105.  
  106.   return uri;
  107. }
  108.  
  109. function needHomepageOverride(prefb) {
  110.   var savedmstone;
  111.   try {
  112.     savedmstone = prefb.getCharPref("browser.startup.homepage_override.mstone");
  113.   }
  114.   catch (e) {
  115.   }
  116.  
  117.   if (savedmstone == "ignore")
  118.     return 0;
  119.  
  120.   var mstone = Components.classes["@mozilla.org/network/protocol;1?name=http"]
  121.                          .getService(nsIHttpProtocolHandler).misc;
  122.  
  123.   if (mstone != savedmstone) {
  124.     prefb.setCharPref("browser.startup.homepage_override.mstone", mstone);
  125.     // Return 1 if true if the pref didn't exist (i.e. new profile) or 2 for an upgrade
  126.     return (savedmstone ? 2 : 1);
  127.   }
  128.   
  129.   // Return 0 if not a new profile and not an upgrade
  130.   return 0;
  131. }
  132.  
  133. // Copies a pref override file into the user's profile pref-override folder,
  134. // and then tells the pref service to reload it's default prefs.
  135. function copyPrefOverride() {
  136.   try {
  137.     var fileLocator = Components.classes["@mozilla.org/file/directory_service;1"]
  138.                                 .getService(Components.interfaces.nsIProperties);
  139.     const NS_APP_EXISTING_PREF_OVERRIDE = "ExistingPrefOverride";
  140.     var prefOverride = fileLocator.get(NS_APP_EXISTING_PREF_OVERRIDE,
  141.                                        Components.interfaces.nsIFile);
  142.     if (!prefOverride.exists())
  143.       return; // nothing to do
  144.  
  145.     const NS_APP_PREFS_OVERRIDE_DIR     = "PrefDOverride";
  146.     var prefOverridesDir = fileLocator.get(NS_APP_PREFS_OVERRIDE_DIR,
  147.                                            Components.interfaces.nsIFile);
  148.  
  149.     // Check for any existing pref overrides, and remove them if present
  150.     var existingPrefOverridesFile = prefOverridesDir.clone();
  151.     existingPrefOverridesFile.append(prefOverride.leafName);
  152.     if (existingPrefOverridesFile.exists())
  153.       existingPrefOverridesFile.remove(false);
  154.  
  155.     prefOverride.copyTo(prefOverridesDir, null);
  156.  
  157.     // Now that we've installed the new-profile pref override file,
  158.     // re-read the default prefs.
  159.     var prefSvcObs = Components.classes["@mozilla.org/preferences-service;1"]
  160.                                .getService(Components.interfaces.nsIObserver);
  161.     prefSvcObs.observe(null, "reload-default-prefs", null);
  162.   } catch (ex) {
  163.     Components.utils.reportError(ex);
  164.   }
  165. }
  166.  
  167. function openWindow(parent, url, target, features, args) {
  168.   var wwatch = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
  169.                          .getService(nsIWindowWatcher);
  170.  
  171.   var argstring;
  172.   if (args) {
  173.     argstring = Components.classes["@mozilla.org/supports-string;1"]
  174.                             .createInstance(nsISupportsString);
  175.     argstring.data = args;
  176.   }
  177.   return wwatch.openWindow(parent, url, target, features, argstring);
  178. }
  179.  
  180. function openPreferences() {
  181.   var features = "chrome,titlebar,toolbar,centerscreen,dialog=no";
  182.   var url = "chrome://browser/content/preferences/preferences.xul";
  183.  
  184.   var win = getMostRecentWindow("Browser:Preferences");
  185.   if (win) {
  186.     win.focus();
  187.   } else {
  188.     openWindow(null, url, "_blank", features);
  189.   }
  190. }
  191.  
  192. function getMostRecentWindow(aType) {
  193.   var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  194.                      .getService(nsIWindowMediator);
  195.   return wm.getMostRecentWindow(aType);
  196. }
  197.  
  198. //@line 203 "/build/buildd/firefox-2.0.0.3+1/browser/components/nsBrowserContentHandler.js"
  199.  
  200. // this returns the most recent non-popup browser window
  201. function getMostRecentBrowserWindow() {
  202.   var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  203.                      .getService(Components.interfaces.nsIWindowMediator);
  204.  
  205. //@line 210 "/build/buildd/firefox-2.0.0.3+1/browser/components/nsBrowserContentHandler.js"
  206.   var win = wm.getMostRecentWindow("navigator:browser", true);
  207.  
  208.   // if we're lucky, this isn't a popup, and we can just return this
  209.   if (win && !win.toolbar.visible) {
  210.     var windowList = wm.getEnumerator("navigator:browser", true);
  211.     // this is oldest to newest, so this gets a bit ugly
  212.     while (windowList.hasMoreElements()) {
  213.       var nextWin = windowList.getNext();
  214.       if (nextWin.toolbar.visible)
  215.         win = nextWin;
  216.     }
  217.   }
  218. //@line 235 "/build/buildd/firefox-2.0.0.3+1/browser/components/nsBrowserContentHandler.js"
  219.  
  220.   return win;
  221. }
  222.  
  223. function doSearch(searchTerm, cmdLine) {
  224.   var ss = Components.classes["@mozilla.org/browser/search-service;1"]
  225.                      .getService(nsIBrowserSearchService);
  226.  
  227.   var submission = ss.defaultEngine.getSubmission(searchTerm, null);
  228.  
  229.   // fill our nsISupportsArray with uri-as-wstring, null, null, postData
  230.   var sa = Components.classes["@mozilla.org/supports-array;1"]
  231.                      .createInstance(Components.interfaces.nsISupportsArray);
  232.  
  233.   var wuri = Components.classes["@mozilla.org/supports-string;1"]
  234.                        .createInstance(Components.interfaces.nsISupportsString);
  235.   wuri.data = submission.uri.spec;
  236.  
  237.   sa.AppendElement(wuri);
  238.   sa.AppendElement(null);
  239.   sa.AppendElement(null);
  240.   sa.AppendElement(submission.postData);
  241.  
  242.   // XXXbsmedberg: use handURIToExistingBrowser to obey tabbed-browsing
  243.   // preferences, but need nsIBrowserDOMWindow extensions
  244.  
  245.   var wwatch = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
  246.                          .getService(nsIWindowWatcher);
  247.  
  248.   return wwatch.openWindow(null, nsBrowserContentHandler.chromeURL,
  249.                            "_blank",
  250.                            "chrome,dialog=no,all" +
  251.                              nsBrowserContentHandler.getFeatures(cmdLine),
  252.                            sa);
  253. }
  254.  
  255. var nsBrowserContentHandler = {
  256.   /* helper functions */
  257.  
  258.   mChromeURL : null,
  259.  
  260.   get chromeURL() {
  261.     if (this.mChromeURL) {
  262.       return this.mChromeURL;
  263.     }
  264.  
  265.     var prefb = Components.classes["@mozilla.org/preferences-service;1"]
  266.                           .getService(nsIPrefBranch);
  267.     this.mChromeURL = prefb.getCharPref("browser.chromeURL");
  268.  
  269.     return this.mChromeURL;
  270.   },
  271.  
  272.   /* nsISupports */
  273.   QueryInterface : function bch_QI(iid) {
  274.     if (!iid.equals(nsISupports) &&
  275.         !iid.equals(nsICommandLineHandler) &&
  276.         !iid.equals(nsIBrowserHandler) &&
  277.         !iid.equals(nsIContentHandler) &&
  278.         !iid.equals(nsIFactory))
  279.       throw Components.errors.NS_ERROR_NO_INTERFACE;
  280.  
  281.     return this;
  282.   },
  283.  
  284.   /* nsICommandLineHandler */
  285.   handle : function bch_handle(cmdLine) {
  286.     if (cmdLine.handleFlag("browser", false)) {
  287.       openWindow(null, this.chromeURL, "_blank",
  288.                  "chrome,dialog=no,all" + this.getFeatures(cmdLine),
  289.                  this.defaultArgs);
  290.       cmdLine.preventDefault = true;
  291.     }
  292.  
  293.     try {
  294.       var remoteCommand = cmdLine.handleFlagWithParam("remote", true);
  295.     }
  296.     catch (e) {
  297.       throw NS_ERROR_ABORT;
  298.     }
  299.  
  300.     if (remoteCommand != null) {
  301.       try {
  302.         var a = /^\s*(\w+)\(([^\)]*)\)\s*$/.exec(remoteCommand);
  303.         var remoteVerb;
  304.         if (a) {
  305.           remoteVerb = a[1].toLowerCase();
  306.           var remoteParams = [];
  307.           var sepIndex = a[2].lastIndexOf(",");
  308.           if (sepIndex == -1)
  309.             remoteParams[0] = a[2];
  310.           else {
  311.             remoteParams[0] = a[2].substring(0, sepIndex);
  312.             remoteParams[1] = a[2].substring(sepIndex + 1);
  313.           }
  314.         }
  315.  
  316.         switch (remoteVerb) {
  317.         case "openurl":
  318.         case "openfile":
  319.           // openURL(<url>)
  320.           // openURL(<url>,new-window)
  321.           // openURL(<url>,new-tab)
  322.  
  323.           // First param is the URL, second param (if present) is the "target"
  324.           // (tab, window)
  325.           var url = remoteParams[0];
  326.           var target = nsIBrowserDOMWindow.OPEN_DEFAULTWINDOW;
  327.           if (remoteParams[1]) {
  328.             var targetParam = remoteParams[1].toLowerCase()
  329.                                              .replace(/^\s*|\s*$/g, "");
  330.             if (targetParam == "new-tab")
  331.               target = nsIBrowserDOMWindow.OPEN_NEWTAB;
  332.             else if (targetParam == "new-window")
  333.               target = nsIBrowserDOMWindow.OPEN_NEWWINDOW;
  334.             else {
  335.               // The "target" param isn't one of our supported values, so
  336.               // assume it's part of a URL that contains commas.
  337.               url += "," + remoteParams[1];
  338.             }
  339.           }
  340.  
  341.           var uri = resolveURIInternal(cmdLine, url);
  342.           handURIToExistingBrowser(uri, target, cmdLine);
  343.           break;
  344.  
  345.         case "xfedocommand":
  346.           // xfeDoCommand(openBrowser)
  347.           if (remoteParams[0].toLowerCase() != "openbrowser")
  348.             throw NS_ERROR_ABORT;
  349.  
  350.           openWindow(null, this.chromeURL, "_blank",
  351.                      "chrome,dialog=no,all" + this.getFeatures(cmdLine),
  352.                      this.defaultArgs);
  353.           break;
  354.  
  355.         default:
  356.           // Somebody sent us a remote command we don't know how to process:
  357.           // just abort.
  358.           throw "Unknown remote command.";
  359.         }
  360.  
  361.         cmdLine.preventDefault = true;
  362.       }
  363.       catch (e) {
  364.         Components.utils.reportError(e);
  365.         // If we had a -remote flag but failed to process it, throw
  366.         // NS_ERROR_ABORT so that the xremote code knows to return a failure
  367.         // back to the handling code.
  368.         throw NS_ERROR_ABORT;
  369.       }
  370.     }
  371.  
  372.     var uriparam;
  373.     try {
  374.       while ((uriparam = cmdLine.handleFlagWithParam("new-window", false))) {
  375.         var uri = resolveURIInternal(cmdLine, uriparam);
  376.         if (!shouldLoadURI(uri))
  377.           continue;
  378.         openWindow(null, this.chromeURL, "_blank",
  379.                    "chrome,dialog=no,all" + this.getFeatures(cmdLine),
  380.                    uri.spec);
  381.         cmdLine.preventDefault = true;
  382.       }
  383.     }
  384.     catch (e) {
  385.       Components.utils.reportError(e);
  386.     }
  387.  
  388.     try {
  389.       while ((uriparam = cmdLine.handleFlagWithParam("new-tab", false))) {
  390.         var uri = resolveURIInternal(cmdLine, uriparam);
  391.         handURIToExistingBrowser(uri, nsIBrowserDOMWindow.OPEN_NEWTAB, cmdLine);
  392.         cmdLine.preventDefault = true;
  393.       }
  394.     }
  395.     catch (e) {
  396.       Components.utils.reportError(e);
  397.     }
  398.  
  399.     var chromeParam = cmdLine.handleFlagWithParam("chrome", false);
  400.     if (chromeParam) {
  401.  
  402.       // Handle the old preference dialog URL separately (bug 285416)
  403.       if (chromeParam == "chrome://browser/content/pref/pref.xul") {
  404.         openPreferences();
  405.       } else {
  406.         var features = "chrome,dialog=no,all" + this.getFeatures(cmdLine);
  407.         openWindow(null, chromeParam, "_blank", features, "");
  408.       }
  409.  
  410.       cmdLine.preventDefault = true;
  411.     }
  412.     if (cmdLine.handleFlag("preferences", false)) {
  413.       openPreferences();
  414.       cmdLine.preventDefault = true;
  415.     }
  416.     if (cmdLine.handleFlag("silent", false))
  417.       cmdLine.preventDefault = true;
  418.  
  419.     var searchParam = cmdLine.handleFlagWithParam("search", false);
  420.     if (searchParam) {
  421.       doSearch(searchParam, cmdLine);
  422.       cmdLine.preventDefault = true;
  423.     }
  424.  
  425. //@line 454 "/build/buildd/firefox-2.0.0.3+1/browser/components/nsBrowserContentHandler.js"
  426.   },
  427.  
  428.   helpInfo : "  -browser            Open a browser window.\n",
  429.  
  430.   /* nsIBrowserHandler */
  431.  
  432.   get defaultArgs() {
  433.     var prefb = Components.classes["@mozilla.org/preferences-service;1"]
  434.                           .getService(nsIPrefBranch);
  435.     var formatter = Components.classes["@mozilla.org/toolkit/URLFormatterService;1"]
  436.                               .getService(Components.interfaces.nsIURLFormatter);
  437.  
  438.     var pagesToLoad = "";
  439.     var overrideState = needHomepageOverride(prefb);
  440.  
  441.     var startpage = "";
  442.     try {
  443.       var choice = prefb.getIntPref("browser.startup.page");
  444.       if (choice == 1)
  445.         startpage = this.startPage;
  446.  
  447.       if (choice == 2)
  448.         startpage = Components.classes["@mozilla.org/browser/global-history;2"]
  449.                               .getService(nsIBrowserHistory).lastPageVisited;
  450.     }
  451.     catch (e) {
  452.     }
  453.  
  454.     if (startpage == "about:blank")
  455.       startpage = "";
  456.  
  457.     if (pagesToLoad && startpage) pagesToLoad += "|";
  458.     pagesToLoad += startpage;
  459.  
  460.     return (pagesToLoad ?  pagesToLoad : "about:blank");
  461.   },
  462.  
  463.   get startPage() {
  464.     var prefb = Components.classes["@mozilla.org/preferences-service;1"]
  465.                           .getService(nsIPrefBranch);
  466.  
  467.     var uri = prefb.getComplexValue("browser.startup.homepage",
  468.                                     nsIPrefLocalizedString).data;
  469.  
  470.     if (!uri) {
  471.       prefb.clearUserPref("browser.startup.homepage");
  472.       uri = prefb.getComplexValue("browser.startup.homepage",
  473.                                   nsIPrefLocalizedString).data;
  474.     }
  475.                                 
  476.     var count;
  477.     try {
  478.       count = prefb.getIntPref("browser.startup.homepage.count");
  479.     }
  480.     catch (e) {
  481.       return uri;
  482.     }
  483.  
  484.     for (var i = 1; i < count; ++i) {
  485.       try {
  486.         var page = prefb.getComplexValue("browser.startup.homepage." + i,
  487.                                          nsIPrefLocalizedString).data;
  488.         uri += "\n" + page;
  489.       }
  490.       catch (e) {
  491.       }
  492.     }
  493.  
  494.     return uri;
  495.   },
  496.  
  497.   mFeatures : null,
  498.  
  499.   getFeatures : function bch_features(cmdLine) {
  500.     if (this.mFeatures === null) {
  501.       this.mFeatures = "";
  502.  
  503.       try {
  504.         var width = cmdLine.handleFlagWithParam("width", false);
  505.         var height = cmdLine.handleFlagWithParam("height", false);
  506.  
  507.         if (width)
  508.           this.mFeatures += ",width=" + width;
  509.         if (height)
  510.           this.mFeatures += ",height=" + height;
  511.       }
  512.       catch (e) {
  513.       }
  514.     }
  515.  
  516.     return this.mFeatures;
  517.   },
  518.  
  519.   /* nsIContentHandler */
  520.  
  521.   handleContent : function bch_handleContent(contentType, context, request) {
  522.     try {
  523.       var webNavInfo = Components.classes["@mozilla.org/webnavigation-info;1"]
  524.                                  .getService(nsIWebNavigationInfo);
  525.       if (!webNavInfo.isTypeSupported(contentType, null)) {
  526.         throw NS_ERROR_WONT_HANDLE_CONTENT;
  527.       }
  528.     } catch (e) {
  529.       throw NS_ERROR_WONT_HANDLE_CONTENT;
  530.     }
  531.  
  532.     var parentWin;
  533.     try {
  534.       parentWin = context.getInterface(nsIDOMWindow);
  535.     }
  536.     catch (e) {
  537.     }
  538.  
  539.     request.QueryInterface(nsIChannel);
  540.     
  541.     openWindow(parentWin, request.URI, "_blank", null, null);
  542.     request.cancel(NS_BINDING_ABORTED);
  543.   },
  544.  
  545.   /* nsIFactory */
  546.   createInstance: function bch_CI(outer, iid) {
  547.     if (outer != null)
  548.       throw Components.results.NS_ERROR_NO_AGGREGATION;
  549.  
  550.     return this.QueryInterface(iid);
  551.   },
  552.     
  553.   lockFactory : function bch_lock(lock) {
  554.     /* no-op */
  555.   }
  556. };
  557.  
  558. const bch_contractID = "@mozilla.org/browser/clh;1";
  559. const bch_CID = Components.ID("{5d0ce354-df01-421a-83fb-7ead0990c24e}");
  560. const CONTRACTID_PREFIX = "@mozilla.org/uriloader/content-handler;1?type=";
  561.  
  562. function handURIToExistingBrowser(uri, location, cmdLine)
  563. {
  564.   if (!shouldLoadURI(uri))
  565.     return;
  566.  
  567.   var navWin = getMostRecentBrowserWindow();
  568.   if (!navWin) {
  569.     // if we couldn't load it in an existing window, open a new one
  570.     openWindow(null, nsBrowserContentHandler.chromeURL, "_blank",
  571.                "chrome,dialog=no,all" + nsBrowserContentHandler.getFeatures(cmdLine),
  572.                uri.spec);
  573.     return;
  574.   }
  575.  
  576.   var navNav = navWin.QueryInterface(nsIInterfaceRequestor)
  577.                      .getInterface(nsIWebNavigation);
  578.   var rootItem = navNav.QueryInterface(nsIDocShellTreeItem).rootTreeItem;
  579.   var rootWin = rootItem.QueryInterface(nsIInterfaceRequestor)
  580.                         .getInterface(nsIDOMWindow);
  581.   var bwin = rootWin.QueryInterface(nsIDOMChromeWindow).browserDOMWindow;
  582.   bwin.openURI(uri, null, location,
  583.                nsIBrowserDOMWindow.OPEN_EXTERNAL);
  584. }
  585.  
  586.  
  587. var nsDefaultCommandLineHandler = {
  588.   /* nsISupports */
  589.   QueryInterface : function dch_QI(iid) {
  590.     if (!iid.equals(nsISupports) &&
  591.         !iid.equals(nsICommandLineHandler) &&
  592.         !iid.equals(nsIFactory))
  593.       throw Components.errors.NS_ERROR_NO_INTERFACE;
  594.  
  595.     return this;
  596.   },
  597.  
  598.   // List of uri's that were passed via the command line without the app
  599.   // running and have already been handled. This is compared against uri's
  600.   // opened using DDE on Win32 so we only open one of the requests.
  601.   _handledURIs: [ ],
  602. //@line 633 "/build/buildd/firefox-2.0.0.3+1/browser/components/nsBrowserContentHandler.js"
  603.  
  604.   /* nsICommandLineHandler */
  605.   handle : function dch_handle(cmdLine) {
  606.     var urilist = [];
  607.  
  608. //@line 659 "/build/buildd/firefox-2.0.0.3+1/browser/components/nsBrowserContentHandler.js"
  609.  
  610.     try {
  611.       var ar;
  612.       while ((ar = cmdLine.handleFlagWithParam("url", false))) {
  613.         var found = false;
  614.         var uri = resolveURIInternal(cmdLine, ar);
  615.         // count will never be greater than zero except on Win32.
  616.         var count = this._handledURIs.length;
  617.         for (var i = 0; i < count; ++i) {
  618.           if (this._handledURIs[i].spec == uri.spec) {
  619.             this._handledURIs.splice(i, 1);
  620.             found = true;
  621.             cmdLine.preventDefault = true;
  622.             break;
  623.           }
  624.         }
  625.         if (!found) {
  626.           urilist.push(uri);
  627.           // The requestpending command line flag is only used on Win32.
  628.           if (cmdLine.handleFlag("requestpending", false) &&
  629.               cmdLine.state == nsICommandLine.STATE_INITIAL_LAUNCH)
  630.             this._handledURIs.push(uri)
  631.         }
  632.       }
  633.     }
  634.     catch (e) {
  635.       Components.utils.reportError(e);
  636.     }
  637.  
  638.     count = cmdLine.length;
  639.  
  640.     for (i = 0; i < count; ++i) {
  641.       var curarg = cmdLine.getArgument(i);
  642.       if (curarg.match(/^-/)) {
  643.         Components.utils.reportError("Warning: unrecognized command line flag " + curarg + "\n");
  644.         // To emulate the pre-nsICommandLine behavior, we ignore
  645.         // the argument after an unrecognized flag.
  646.         ++i;
  647.       } else {
  648.         try {
  649.           urilist.push(resolveURIInternal(cmdLine, curarg));
  650.         }
  651.         catch (e) {
  652.           Components.utils.reportError("Error opening URI '" + curarg + "' from the command line: " + e + "\n");
  653.         }
  654.       }
  655.     }
  656.  
  657.     if (urilist.length) {
  658.       if (cmdLine.state != nsICommandLine.STATE_INITIAL_LAUNCH &&
  659.           urilist.length == 1) {
  660.         // Try to find an existing window and load our URI into the
  661.         // current tab, new tab, or new window as prefs determine.
  662.         try {
  663.           handURIToExistingBrowser(urilist[0], nsIBrowserDOMWindow.OPEN_DEFAULTWINDOW, cmdLine);
  664.           return;
  665.         }
  666.         catch (e) {
  667.         }
  668.       }
  669.  
  670.       var speclist = [];
  671.       for (uri in urilist) {
  672.         if (shouldLoadURI(urilist[uri]))
  673.           speclist.push(urilist[uri].spec);
  674.       }
  675.  
  676.       if (speclist.length) {
  677.         openWindow(null, nsBrowserContentHandler.chromeURL, "_blank",
  678.                    "chrome,dialog=no,all" + nsBrowserContentHandler.getFeatures(cmdLine),
  679.                    speclist.join("|"));
  680.       }
  681.  
  682.     }
  683.     else if (!cmdLine.preventDefault) {
  684.       openWindow(null, nsBrowserContentHandler.chromeURL, "_blank",
  685.                  "chrome,dialog=no,all" + nsBrowserContentHandler.getFeatures(cmdLine),
  686.                  nsBrowserContentHandler.defaultArgs);
  687.     }
  688.   },
  689.  
  690.   // XXX localize me... how?
  691.   helpInfo : "Usage: firefox [-flags] [<url>]\n",
  692.  
  693.   /* nsIFactory */
  694.   createInstance: function dch_CI(outer, iid) {
  695.     if (outer != null)
  696.       throw Components.results.NS_ERROR_NO_AGGREGATION;
  697.  
  698.     return this.QueryInterface(iid);
  699.   },
  700.     
  701.   lockFactory : function dch_lock(lock) {
  702.     /* no-op */
  703.   }
  704. };
  705.  
  706. const dch_contractID = "@mozilla.org/browser/final-clh;1";
  707. const dch_CID = Components.ID("{47cd0651-b1be-4a0f-b5c4-10e5a573ef71}");
  708.  
  709. var Module = {
  710.   /* nsISupports */
  711.   QueryInterface: function mod_QI(iid) {
  712.     if (iid.equals(Components.interfaces.nsIModule) ||
  713.         iid.equals(Components.interfaces.nsISupports))
  714.       return this;
  715.  
  716.     throw Components.results.NS_ERROR_NO_INTERFACE;
  717.   },
  718.  
  719.   /* nsIModule */
  720.   getClassObject: function mod_getco(compMgr, cid, iid) {
  721.     if (cid.equals(bch_CID))
  722.       return nsBrowserContentHandler.QueryInterface(iid);
  723.  
  724.     if (cid.equals(dch_CID))
  725.       return nsDefaultCommandLineHandler.QueryInterface(iid);
  726.  
  727.     throw Components.results.NS_ERROR_NO_INTERFACE;
  728.   },
  729.     
  730.   registerSelf: function mod_regself(compMgr, fileSpec, location, type) {
  731.     var compReg =
  732.       compMgr.QueryInterface( Components.interfaces.nsIComponentRegistrar );
  733.  
  734.     compReg.registerFactoryLocation( bch_CID,
  735.                                      "nsBrowserContentHandler",
  736.                                      bch_contractID,
  737.                                      fileSpec,
  738.                                      location,
  739.                                      type );
  740.     compReg.registerFactoryLocation( dch_CID,
  741.                                      "nsDefaultCommandLineHandler",
  742.                                      dch_contractID,
  743.                                      fileSpec,
  744.                                      location,
  745.                                      type );
  746.  
  747.     function registerType(contentType) {
  748.       compReg.registerFactoryLocation( bch_CID,
  749.                                        "Browser Cmdline Handler",
  750.                                        CONTRACTID_PREFIX + contentType,
  751.                                        fileSpec,
  752.                                        location,
  753.                                        type );
  754.     }
  755.  
  756.     registerType("text/html");
  757.     registerType("application/vnd.mozilla.xul+xml");
  758. //@line 809 "/build/buildd/firefox-2.0.0.3+1/browser/components/nsBrowserContentHandler.js"
  759.     registerType("image/svg+xml");
  760. //@line 811 "/build/buildd/firefox-2.0.0.3+1/browser/components/nsBrowserContentHandler.js"
  761.     registerType("text/rdf");
  762.     registerType("text/xml");
  763.     registerType("application/xhtml+xml");
  764.     registerType("text/css");
  765.     registerType("text/plain");
  766.     registerType("image/gif");
  767.     registerType("image/jpeg");
  768.     registerType("image/jpg");
  769.     registerType("image/png");
  770.     registerType("image/bmp");
  771.     registerType("image/x-icon");
  772.     registerType("image/vnd.microsoft.icon");
  773.     registerType("image/x-xbitmap");
  774.     registerType("application/http-index-format");
  775.  
  776.     var catMan = Components.classes["@mozilla.org/categorymanager;1"]
  777.                            .getService(nsICategoryManager);
  778.  
  779.     catMan.addCategoryEntry("command-line-handler",
  780.                             "m-browser",
  781.                             bch_contractID, true, true);
  782.     catMan.addCategoryEntry("command-line-handler",
  783.                             "x-default",
  784.                             dch_contractID, true, true);
  785.   },
  786.     
  787.   unregisterSelf : function mod_unregself(compMgr, location, type) {
  788.     var compReg = compMgr.QueryInterface(nsIComponentRegistrar);
  789.     compReg.unregisterFactoryLocation(bch_CID, location);
  790.     compReg.unregisterFactoryLocation(dch_CID, location);
  791.  
  792.     var catMan = Components.classes["@mozilla.org/categorymanager;1"]
  793.                            .getService(nsICategoryManager);
  794.  
  795.     catMan.deleteCategoryEntry("command-line-handler",
  796.                                "m-browser", true);
  797.     catMan.deleteCategoryEntry("command-line-handler",
  798.                                "x-default", true);
  799.   },
  800.  
  801.   canUnload: function(compMgr) {
  802.     return true;
  803.   }
  804. };
  805.  
  806. // NSGetModule: Return the nsIModule object.
  807. function NSGetModule(compMgr, fileSpec) {
  808.   return Module;
  809. }
  810.